<?php
declare(strict_types=1);

namespace App\ZxAdmin\Sys;

use App\Controller;

/**
 * @title    系统设置
 * @auth    true
 * @login    true
 * @menu    true
 * @component vlist/form.vue
 * @icon el-icon-menu
 * @rank    99
 */
// hook zxadmin_sys_plugin_use.php

class Config extends Controller
{
    // hook zxadmin_sys_plugin_use.php

    public $Tabs = [
        [
            'Name' => '基本',
            'Index' => 'base'
        ],
        [
            'Name' => '系统',
            'Index' => 'system'
        ],
        [
            'Name' => '第三方',
            'Index' => 'open'
        ],
        [
            'Name' => '上传',
            'Index' => 'upload'
        ], [
            'Name' => '会员',
            'Index' => 'user'
        ], [
            'Name' => '订单',
            'Index' => 'order'
        ], [
            'Name' => '通讯',
            'Index' => 'socket'
        ],
    ];

    /**
     * @title  配置下发
     * @rank    99
     */
    public function action_Conf_PUT()
    {
        // hook controller_zxadmin_sys_table_index_start.php
        $formItems = [
            'base' => [
                [
                    'label' => "站点名称",
                    'prop' => "Title",
                    'span' => 12,
                    'component' => 'input',
                    'options' => ['maxlength' => 128, 'minlength' => 2,'placeholder' => '请输入站点名称'],
                    'rules' => [
                        ['required' => true, 'message' => '请输入站点名称']
                    ],
                    'requiredHandle' => "$.required==true"
                ],
                [
                    'label' => "SessionID",
                    'prop' => "session_id",
                    'span' => 12,
                    'component' => 'input',
                    'options' => ['maxlength' => 32, 'minlength' => 6, 'placeholder' => '请输入SessionID'],
                    'rules' => [
                        ['required' => true, 'message' => '请输入SessionID']
                    ],
                    'requiredHandle' => "$.required==true"
                ],
                [
                    'label' => "加密密钥",
                    'prop' => "auth_key",
                    'span' => 12,
                    'makeType' => 'random',
                    'component' => 'input',
                    'options' => ['length' => 32, 'placeholder' => '请输入加密密钥'],
                    'rules' => [
                        ['required' => true, 'message' => '请输入加密密钥']
                    ],
                    'requiredHandle' => "$.required==true"
                ],
                [
                    'label' => "并发限制",
                    'prop' => "limiting",
                    'span' => 12,
                    'component' => 'input',
                    'rules' => [
                        ['required' => true, 'message' => '请输入并发限制']
                    ],
                    'requiredHandle' => "$.required==true"

                ], [
                    'label' => "调试模式",
                    'prop' => "debug",
                    'span' => 12,
                    'component' => 'radioGroup',
                    'dict' => 'Status',
                    'noall' => 1,
                ],],
            'open' => [
                [
                    'label' => "微信AppID",
                    'prop' => "WechatAppID",
                    'span' => 12,
                    'component' => 'input',
                ],
                [
                    'label' => "微信AppSecret",
                    'prop' => "WechatAppSecret",
                    'span' => 12,
                    'component' => 'input',
                ], [
                    'label' => "微信Token",
                    'prop' => "WechatToken",
                    'span' => 12,
                    'component' => 'input',
                    'makeType' => 'random',
                ],
                [
                    'label' => "微信EAKey",
                    'prop' => "WechatEncodingAesKey",
                    'span' => 12,
                    'component' => 'input',
                    'makeType' => 'random',
                    'length' => 40,
                ],],
            'upload' => [
                [
                    'label' => "OssBucket",
                    'prop' => "OssBucket",
                    'span' => 12,
                    'component' => 'input',
                ],
                [
                    'label' => "OssHost",
                    'prop' => "OssHost",
                    'span' => 12,
                    'component' => 'input',
                ],
                [
                    'label' => "OssEndpoint",
                    'prop' => "OssEndpoint",
                    'span' => 12,
                    'component' => 'input',
                ],
                [
                    'label' => "OssAccessKey",
                    'prop' => "OssAccessKey",
                    'span' => 12,
                    'component' => 'input',
                    'makeType' => 'random',
                ],
                [
                    'label' => "OssSecretKey",
                    'prop' => "OssSecretKey",
                    'span' => 12,
                    'component' => 'input',
                    'makeType' => 'random',
                ],
            ]
        ];

        // hook controller_zxadmin_sys_table_index_end.php
        $this->response('0000', ['Tabs' => $this->Tabs, 'formItems' => $formItems]);
    }

    /**
     * @title   配置字段
     * @rank    60
     */
    public function action_Index_GET()
    {
        $this->response('0000', ['data' => $_ENV['conf']]);
    }

    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Index_POST()
    {
        // hook controller_zxadmin_content_cate_field_post_start.php
        $data = $this->request->post();

        foreach ($data as $k => $v) {
            if (!isset($_ENV['conf'][$k])) {
                unset($data[$k]);
            }
        }
        $r = file_save_var($data, __SERDIR__ . __LV__ . '_config.php');
        if ($r) {
            $_ENV['conf'] = include _include(__SERDIR__ . __LV__ . '_config.php');
            $this->SysUser->sendMessage('reloadConfig', []);
        }
        // hook controller_zxadmin_content_cate_field_post_end.php
        $this->response($r ? '0000' : '0001', [], $r ? '保存成功' : '保存失败');
    }

    // hook zxadmin_sys_plugin_end.php
}

?>